package IO;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-11-02
 * Time: 19:01
 */
public class FileCopy {
    public static void main(String[] args) throws IOException{
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件路径: ");
        String srcPath = scanner.next();
        System.out.println("请输入目标文件路径: ");
        String destPath = scanner.next();
        File srcFile = new File(srcPath);
        if (!srcFile.isFile()) {
            System.out.println("源文件不存在或不是文件");
            return;
        }

        //保证目标文件的父级目录路径存在
        File path = new File(destPath);
        String parent = path.getParent();
        System.out.println(parent);
        File parentPath = new File(parent);
        if(!parentPath.exists()){
            System.out.println("该目标文件父目录不存在");
        }

        try(InputStream inputStream = new FileInputStream(srcPath);
            OutputStream outputStream = new FileOutputStream(destPath)){

            while(true){
                byte[] ret = new byte[1024];
                //读取源文件内容
                int n = inputStream.read(ret);
                if(n == -1){
                    break;
                }

                //读入目标文件
                outputStream.write(ret,0,n);
            }
        }
    }
}
